<template>
  <div id="app">
    <div class="chat-container">
      <div class="chat-header">
        <h1 class="title">智能客服</h1>
      </div>
      <div class="chat-window">
        <div v-for="(message, index) in messages" :key="index" class="message">
          <div class="message-content" :class="{ 'user': message.sender === 'user', 'bot': message.sender === 'bot' }">
            <span>{{ message.text }}</span>
          </div>
        </div>
      </div>
      <div class="input-container">
        <input v-model="inputText" @keyup.enter="sendMessage" placeholder="请输入你的问题">
        <button @click="sendMessage">发送</button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'App',
  data() {
    return {
      messages: [],
      inputText: ''
    };
  },
  methods: {
    async sendMessage() {
      if (this.inputText.trim() === '') return;
      const userMessage = { sender: 'user', text: this.inputText };
      this.messages.push(userMessage);
      this.inputText = '';

      try {
        const response = await axios.post('http://localhost:3000/chat', {
          message: userMessage.text
        });
        const botMessage = { sender: 'bot', text: response.data.answer };
        this.messages.push(botMessage);
      } catch (error) {
        console.error('请求出错:', error);
      }
    }
  }
};
</script>

<style scoped>
/* 整体容器样式 */
#app {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('../src/assets/bg3.png');
  background-color: #ffffff; 
}

.chat-container {
  width: 900px;
  height: 600px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  box-shadow: 0 0 30px rgba(255, 202, 228, 0.8); 
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 2px solid rgba(255, 202, 228, 0.8); 
}

.chat-header {
  background-color: rgba(249, 142, 169, 0.639); 
  padding: 15px;
  text-align: center;
  border-bottom: 2px solid rgba(255, 105, 180, 0.8); 
}

.title {
  color: #e37bac;
  margin: 0;
  font-size: 24px;
  text-shadow: 0 0 15px #ffacd6; 
}

/* 聊天窗口样式 */
.chat-window {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  background-color: rgb(255, 255, 255); 
}

.message {
  margin-bottom: 15px;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-content {
  padding: 12px 18px;
  border-radius: 20px;
  max-width: 40%;
  position: relative;
  box-shadow: 0 0 15px rgb(247, 180, 214); 
}

.user {
  background-color: rgb(255, 255, 255); 
  align-self: flex-end;
  border: 2px solid rgba(255, 169, 212, 0.8);
  color: rgb(247, 180, 214);
}

.bot {
  background-color: rgb(255, 255, 255); 
  align-self: flex-start;
  border: 2px solid rgba(255, 105, 180, 0.5); 
}
.input-container {
  padding: 15px;
  display: flex;
  border-top: 2px solid rgba(254, 192, 223, 0.8); 
  background-color: rgb(255, 255, 255); 
}

input {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 20px;
  background-color: rgb(255, 255, 255); 
  margin-right: 10px;
  outline: none;
  color: rgb(240, 145, 194);
  box-shadow: 0 0 15px rgba(255, 105, 180, 0.5); 
}

input::placeholder {
  color: rgba(238, 140, 189, 0.6);
}

button {
  padding: 12px 20px;
  border: none;
  border-radius: 20px;
  background-color: rgba(255, 105, 180, 0.8); 
  color: white;
  cursor: pointer;
  transition: background-color 0.3s ease;
  box-shadow: 0 0 15px rgba(255, 105, 180, 0.8); 
}

button:hover {
  background-color: rgba(255, 105, 180, 1);
}
</style>